﻿//----------------------------------------------------------------
// <copyright file="MptParentChildReceiver.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System.ServiceModel;
using System.ServiceModel.Web;
using MptCodeLibrary.Configuration;
using MptCodeLibrary.Exceptions;
using MptCodeLibrary.TeamFoundation;

namespace MptCodeLibrary.WcfReceivers
{
    [ServiceContract]
    public interface IMptParentChildReceiver
    {
        [OperationContract]
        [WebGet]
        void CheckAndRebuildGlobalLists();

        [OperationContract]
        [WebGet]
        void IncrementalProcess();
    }

    public class MptParentChildReceiver : IMptParentChildReceiver
    {
        public void CheckAndRebuildGlobalLists()
        {
            try
            {
                MptEventLogger.WriteTrace("Starting to rebuild the global list via a WCF request");
                
                GlobalListBuilder globalListBuilder = new GlobalListBuilder(new MptConfiguration(),
                                                                            new TeamFoundationFacade());
                globalListBuilder.RebuildGlobalListsForProjects();

                MptEventLogger.WriteTrace("Finished rebuilding the global list via a WCF request");
            }
            catch (MptException mptException)
            {
                MptEventLogger.WriteError("Could not rebuild the global lists.  The error is: " +
                                          mptException);
            }
        }

        public void IncrementalProcess()
        {
            try
            {
                MptEventLogger.WriteTrace("Starting to incremental rollup process via a WCF request");

                WorkItemRollup workItemRollup = new WorkItemRollup(new MptConfiguration());
                workItemRollup.IncrementalProcess();

                MptEventLogger.WriteTrace("Finished the incremental rollup process via a WCF request");
            }
            catch (MptException mptException)
            {
                MptEventLogger.WriteError("Could not incrementally rollup the work items.  The error is: " +
                                          mptException);
            }
        }
    }
}